<?php
/* Author:bigeyex@gmail.com
 * this file handles database tasks in PLF.
 *
 */
class PLFDao{

	private static $_conn;
	
    //create new connection.
    //@param $host_name
    //@param $user_name
    //@param $password
    //@param $database_name
	public static function set($host_name,$user_name,$password,$database_name){
		self::$_conn=new mysqli($host_name,$user_name,$password,$database_name);
	}

    //get the database connection
	public static function get(){
		return self::$_conn;
    }

    public function close(){
        self::$_conn->close();
    }

    //query an sql statement
	public function query($sql){
		return self::$_conn->query($sql);
	}

    //insert data into a table
    //@param $table:table to be inserted
    //@param $values:array of the data.
    //eg.
    //add("user",
    //      array(
    //          'username'=>'somebody',
    //          'password'=>'123'
    //          )
    //  )
	public function add($table,$values){
		return self::$_conn->query("insert into " . $table . " (" .
			 implode(',',array_keys($values)) . ") values ('" . implode("','",$values) . "')");
	}
	
	public function delete($table,$where_clause){
		return self::$_conn->query("delete from " . $table . " where " . $where_clause);
	}
	
	public function update($table,$values,$where_clause){
		$sets=array();
		foreach($values as $key=>$value){
			$sets[]=$key . "='" . $value . "'";
        }
        echo "update " . $table . " set " . implode(",",$sets) .
			 " where " . $where_clause;
		return self::$_conn->query("update " . $table . " set " . implode(",",$sets) .
			 " where " . $where_clause); 
	}
	
	public function delete_id($table,$id){
		return $this->delete($table,"id=" . $id);
	}
	
	public function update_id($table,$values,$id){
		return $this->update($table,$values,"id=" . $id);
	}
}

?>
